From 52a7c094074e85bfe24bc17b5e51cb8218685508 Mon Sep 17 00:00:00 2001 From: robertl Date: Sun, 11 Mar 2007 15:33:55 +0000 Subject: [PATCH] Add multiple link support for GPX 1.1 on read and write. --- defs.h | 21 +++++++++++++++------ gpx.c | 45 +++++++++++++++++++++++++++++++++------------ util.c | 2 +- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/defs.h b/defs.h index 3bfb8b31e..527c1b025 100644 --- a/defs.h +++ b/defs.h @@ -276,12 +276,6 @@ typedef struct { unsigned int cet_converted:1; /* strings are converted to UTF8; interesting only for input */ } wp_flags; -typedef struct url_link { - struct url_link *url_next; - char *url; - char *url_link_text; -} url_link; - /* * This is a waypoint, as stored in the GPSR. It tries to not * cater to any specific model or protocol. Anything that needs to @@ -336,6 +330,8 @@ typedef struct { * afterthought and I don't want to change our data structures. * So we have the first in the waypoint itself and subsequent * ones in a linked list. + * We also use an implicit anonymous union here, so these three + * members must match struct url_link... */ struct url_link *url_next; char *url; @@ -421,6 +417,19 @@ typedef struct { int request_terminate; } posn_status; +/* + * Structures and functions for multiple URLs per waypoint. + */ +typedef struct url_link { + struct url_link *url_next; + char *url; + char *url_link_text; +} url_link; + +void add_url(waypoint *wpt, char *link, char *url_link_text); + + + typedef void (*ff_init) (char const *); typedef void (*ff_deinit) (void); typedef void (*ff_read) (void); diff --git a/gpx.c b/gpx.c index b204f5f5a..0dfeda323 100644 --- a/gpx.c +++ b/gpx.c @@ -48,6 +48,8 @@ static int cache_descr_is_html; static gbfile *fd; static gbfile *ofd; static short_handle mkshort_handle; +static const char *link_url; +static char *link_text; static const char *input_string = NULL; static int input_string_len = 0; @@ -599,9 +601,12 @@ gpx_start(void *data, const XML_Char *xml_el, const XML_Char **xml_attr) break; case tt_wpt_link: if (0 == strcmp(attr[0], "href")) { - wpt_tmp->url = xstrdup(attr[1]); + link_url = attr[1]; } break; + case tt_wpt_link_text: + link_text = cdatastr.mem; + break; case tt_rte: rte_head = route_head_alloc(); route_add_head(rte_head); @@ -853,9 +858,19 @@ gpx_end(void *data, const XML_Char *xml_el) wpt_tmp->url = xstrdup(cdatastrp); break; case tt_wpt_urlname: - case tt_wpt_link_text: wpt_tmp->url_link_text = xstrdup(cdatastrp); break; + case tt_wpt_link: { + char *lt = link_text; + if (lt) { + lt = xstrdup(lrtrim(link_text)); + } + + fprintf(stderr, "Here %s/%s\n", link_url, lt); + add_url(wpt_tmp, xstrdup(link_url), lt); + link_text = NULL; + } + break; case tt_wpt: waypt_add(wpt_tmp); logpoint_ct = 0; @@ -1396,21 +1411,27 @@ write_gpx_url(const waypoint *waypointp) { char *tmp_ent; - if (waypointp->url) { - tmp_ent = xml_entitize(waypointp->url); - if (gpx_wversion_num > 10) { - + if (waypointp->url == NULL) { + return; + } + + if (gpx_wversion_num > 10) { + url_link *tail; + for (tail = (url_link *)&waypointp->url_next; tail; tail = tail->url_next) { + tmp_ent = xml_entitize(tail->url); gbfprintf(ofd, " \n", urlbase ? urlbase : "", tmp_ent); write_optional_xml_entity(ofd, " ", "text", - waypointp->url_link_text); + tail->url_link_text); gbfprintf(ofd, " \n"); - } else { - gbfprintf(ofd, " %s%s\n", - urlbase ? urlbase : "", tmp_ent); - write_optional_xml_entity(ofd, " ", "urlname", - waypointp->url_link_text); + xfree(tmp_ent); } + } else { + tmp_ent = xml_entitize(waypointp->url); + gbfprintf(ofd, " %s%s\n", + urlbase ? urlbase : "", tmp_ent); + write_optional_xml_entity(ofd, " ", "urlname", + waypointp->url_link_text); xfree(tmp_ent); } } diff --git a/util.c b/util.c index 7d32a2eb1..135574c68 100644 --- a/util.c +++ b/util.c @@ -1655,7 +1655,7 @@ add_url(waypoint *wpt, char *link, char *url_link_text) new_link->url = link; new_link->url_link_text = url_link_text; - /* Find current end of chain. */ + /* Find current end of chain and tack this onto the end.. */ for (tail = wpt->url_next;;tail = tail->url_next) { if (tail == NULL) { wpt->url_next = new_link; -- 2.30.2